package com.mstar.qs.common;

import java.util.Date;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;

public class UtilDateConverter implements Converter {
	public UtilDateConverter()  
    {  
        this.defaultValue = null;  
        this.useDefault = false;  
    }  
  
    public UtilDateConverter(Object defaultValue)  
    {  
        this.defaultValue = defaultValue;  
        this.useDefault = true;  
    }  
  
    private Object defaultValue = null;  
  
    private boolean useDefault = true;  
  
    public Object convert(Class type, Object value)  
    {  
        if( value == null || "".equals(value) )  
        {  
            if( useDefault )  
            {  
                return(defaultValue);  
            }  
            else  
            {  
                throw new ConversionException("No value specified");  
            }  
        }  
  
        if( value instanceof Date )  
        {  
            return(value);  
        }  
  
        try  
        {  
//            return(CalendarUtil.parseDate(value.toString()));
        	return null;
        }  
        catch( Exception e )  
        {  
            if( useDefault )  
            {  
                return(defaultValue);  
            }  
            else  
            {  
                throw new ConversionException(e);  
            }  
        }  
    }  

}
